package kxfang.com.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import kxfang.com.base.fragment.MvvmLazyFragment;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.common.bean.MainCodeModel;
import kxfang.com.common.bean.MeSumModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.parameter.CommonPar;
import kxfang.com.common.router.RoutHub;
import kxfang.com.common.utils.HawkUtil;
import kxfang.com.common.utils.MyUtils;
import kxfang.com.common.utils.RouterUtils;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;
import kxfang.com.user.R;
import kxfang.com.user.acitvity.set.EditInformationActivity;
import kxfang.com.user.acitvity.set.SettingActivity;
import kxfang.com.user.databinding.UserFragmentMeBinding;

@Route(path = RoutHub.PAGER_USER)
public class MeFragment extends MvvmLazyFragment<UserFragmentMeBinding, IMvvmBaseViewModel> {

    private CommonPar par = new CommonPar();
    private int ctype, statu;//statu 行业类型 1 商家 2 店铺
    private int isstore;//isstore 是否为商家 不是商家 = 1 审核中 = 2, 审核失败 = 3, 是商家 = 4 ,
    private int smtype;//  smtype 商户是否实名 未实名 = 1 实名中 = 2, 实名失败 = 3, 已实名 = 4 ,
    private int userType;//用户是否实名 未实名 = 1 实名中 = 2, 实名失败 = 3, 已实名 = 4 ,
    private View alertDialogView;
    AlertDialog.Builder dialog;
    AlertDialog dialog1;
    String allPrice, todayPrice, MyMoney;
    int yqNum, userId;
    private boolean isJr = false;
    String Identifi, OrderNo;//表示是我们平台 和订单号
    private String userCmemo = "", storeCmemo = "";
    private MainCodeModel QrCodeList;

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        StatusBarUtil.setTopFragmentView(this, viewDataBinding.topView,R.color.common_color_F7F7F7);
        viewDataBinding.refreshLayout.setEnableLoadMore(false);
        initView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_fragment_me;
    }

    private void initData(CommonPar par) {
        par.setRUserID(String.valueOf(HawkUtil.getUserId()));
        ctype = Hawk.get("ctype") == null ? 0 : Hawk.get("ctype");
        EasyHttp.post(HttpConfig.getSum)
                .cacheKey(HttpConfig.getSum)
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<MeSumModel>() {
                    @Override
                    public void onSuccess(MeSumModel model) {
//                        MyMoney = model.getData().getAccountBalance();
                        if (!TextUtils.isEmpty(model.getPicUrl())) {
                            Hawk.put("head", model.getPicUrl());
                            Logger.d(model.getPicUrl());
                            Glide.with(getActivity()).load(model.getPicUrl()).error(R.drawable.default_head).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewDataBinding.myHeadImage);
                        } else {
                            Glide.with(getActivity()).load(R.drawable.default_head).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewDataBinding.myHeadImage);

                        }
                        statu = model.getIndustryValue();
                        viewDataBinding.userNmae.setText(model.getAlias());
                        Hawk.put("alias", model.getAlias());
                        isstore = model.getBusinessIsAuth();
                        smtype = model.getBusinessIsAuth();
                        userType = model.getCustomerIsAuth();
                        userCmemo = model.getPersonCmemo();
                        storeCmemo = model.getStoreCmemo();
                        //邀请奖励
                        allPrice = model.getReferrReward();
                        //邀请数量
                        yqNum = model.getInviteCount();
                        //今日邀请奖励
                        todayPrice = model.getTodayInvite();

                        switch (ctype) {
                            case 0:
                                viewDataBinding.userNmae.setText("点击登录");
                                viewDataBinding.dataText.setText("好房好服务 找房上可心");
                                break;
                            case 4:
                                break;
                        }

                    }
                    @Override
                    public void onError(ApiException e) {

                    }
                    @Override
                    public void onCompleted() {
                        viewDataBinding.refreshLayout.finishRefresh();
                    }
                });

    }

    private void initView() {
        Intent intent = new Intent();
        //刷新
        viewDataBinding.refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            if (HawkUtil.getUserId() != null) {
                initData(par);
            }
        });

        viewDataBinding.myRg1.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.my_rb_shagnjia) {//商家服务页面
                viewDataBinding.myRbShagnjia.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
                switch (smtype) {
                    case 1:
                        // 未实名 = 1
//                            intent.setClass(getActivity(), JoinUsActivity.class);
//                            startActivity(intent);
                        break;
                    case 2:
                        //实名中 = 2
//                            intent.setClass(getActivity(), ShenHeActivity.class);
//                            intent.putExtra("bs", 4);
//                            intent.putExtra("type", "2");
//                            startActivity(intent);
                        break;
                    case 3:
                        //实名失败 = 3,
//                            intent.setClass(getActivity(), ShenHeActivity.class);
//                            intent.putExtra("bs", 4);
//                            intent.putExtra("type", "3");
//                            intent.putExtra("content", storeCmemo);
//                            startActivity(intent);
                        break;
                    case 4:
                        //已实名 = 4
//                            intent.setClass(getActivity(), StoreMainActivity.class);
//                            intent.putExtra("status", statu);
//                            startActivity(intent);
                        break;
                    case 5:
                        ToastUtils.showShort("店铺已被禁用，请联系客服咨询 ");
                        break;
                    default:
                        ToastUtils.showShort("请刷新界面");
                        break;
                }
            } else if (checkedId == R.id.my_rb_address) {//地址管理
                viewDataBinding.myRbAddress.setChecked(false);
                viewDataBinding.myRbJingjiren.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getContext());
                    return;
                }
//                Navigate.push(getActivity(), AddressManageFragment.class);
            } else if (checkedId == R.id.my_rb_shiming) {//实名认证页面
                viewDataBinding.myRbShiming.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getContext());
                    return;
                }
//                Navigate.push(getActivity(), CommentListFragment.class);
            } else if (checkedId == R.id.my_rb_tuiguang) {//推广页面
                viewDataBinding.myRbTuiguang.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
                if (userType == 1) {
//                    intent.setClass(getActivity(), RenZActivity.class);
//                    startActivity(intent);
                } else if (userType == 2) {
//                    intent.setClass(getActivity(), ShenHeActivity.class);
//                    intent.putExtra("type", "2");
//                    intent.putExtra("bs", 2);
//                    startActivity(intent);
                } else if (userType == 3) {
//                    intent.setClass(getActivity(), ShenHeActivity.class);
//                    intent.putExtra("type", "3");
//                    intent.putExtra("bs", 2);
//                    intent.putExtra("content", userCmemo);
//                    startActivity(intent);
                } else {
//                    intent.setClass(getActivity(), ShenHeActivity.class);
//                    intent.putExtra("type", "4");
//                    intent.putExtra("bs", 2);
//                    startActivity(intent);
                }
            }
        });
        viewDataBinding.myRg2.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.my_rb_jingjiren) {//我的店铺
                viewDataBinding.myRbJingjiren.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
//                Intent inten1 = new Intent(getContext(), StoreMyQrcodeWebActivity.class);
//                inten1.putExtra("type", 2);
//                inten1.putExtra("userId", HawkUtil.getUserId() + "");
//                startActivity(inten1);
            } else if (checkedId == R.id.my_rb_weituo) {//我的房源
                viewDataBinding.myRbWeituo.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
//                intent.setClass(getActivity(), MyHouseActivity.class);
//                startActivity(intent);
            } else if (checkedId == R.id.my_rb_collection) {//我的委托
                viewDataBinding.myRbCollection.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
//                intent.setClass(getActivity(), CommissionActivity.class);
//                startActivity(intent);
            } else if (checkedId == R.id.my_rb_history) {//我的举报
                viewDataBinding.myRbHistory.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
//                intent.setClass(getActivity(), ReportActivity.class);
//                startActivity(intent);
            }
        });
        viewDataBinding.myRg3.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.my_rb_house) {//关注房源
                viewDataBinding.myRbHouse.setChecked(false);
                if (HawkUtil.getUserId() == null) {
                    MyUtils.myShowDialog(getActivity());
                    return;
                }
//                startActivity(new Intent(getActivity(), AttentionActivity.class));
            } else if (checkedId == R.id.my_rb_jubao) {//商家入驻
                viewDataBinding.myRbJubao.setChecked(false);
//                startActivity(new Intent(getActivity(), JoinUsActivity.class));
            } else if (checkedId == R.id.my_rb_xf) {//帮助与反馈
                viewDataBinding.myRbXf.setChecked(false);
//                Navigate.pushActivity(getContext(), FeedBackActivity.class);
            } else if (checkedId == R.id.my_rb_dp) {//客服中心
                viewDataBinding.myRbDp.setChecked(false);
//                toCustomerService();
            }
        });
        //编辑个人资料
        viewDataBinding.bianjiLayout.setOnClickListener(v -> {
            if (HawkUtil.getUserId() != null) {
                intent.setClass(getActivity(), EditInformationActivity.class);
                startActivity(intent);
            } else {

                RouterUtils.navigate(RoutHub.PAGER_LOGIN);
            }

        });
        //进入设置页面
        viewDataBinding.settingImagesButton.setOnClickListener(v -> {
            intent.setClass(getActivity(), SettingActivity.class);
            startActivity(intent);
        });
        //商家入驻
//        viewDataBinding.ivJoinUs.setOnClickListener(v -> startActivity(new Intent(getActivity(), JoinUsActivity.class)));
        //收藏的店
        viewDataBinding.llCollectStore.setOnClickListener(v->{
            if (HawkUtil.getUserId() == null) {
                MyUtils.myShowDialog(getActivity());
                return;
            }
//            intent.setClass(getActivity(), CollectionActivity.class);
//            startActivity(intent);
        });
        //浏览足迹
        viewDataBinding.llFootprint.setOnClickListener(v->{
            if (HawkUtil.getUserId() == null) {
                MyUtils.myShowDialog(getActivity());
                return;
            }
//            intent.setClass(getActivity(), HistoryActivity.class);
//            startActivity(intent);
        });
        //卡券
        viewDataBinding.llCard.setOnClickListener(v->{
            if (HawkUtil.getUserId() == null) {
                MyUtils.myShowDialog(getActivity());
                return;
            }
//            intent.setClass(getActivity(), MyCardActivity.class);
//            startActivity(intent);
        });
        onClick();
    }
    //点击
    private void onClick(){
        if (HawkUtil.getUserId() == null) {
            MyUtils.myShowDialog(getContext());
            return;
        }
        Bundle bundle = new Bundle();
        viewDataBinding.toOrderDetail.setOnClickListener(view -> {
            bundle.putInt("pos",0);
            //跳转MyOrderActivity.class
        });
        viewDataBinding.countDfk.setOnClickListener(view -> {
            bundle.putInt("pos",1);
        });
        viewDataBinding.countDps.setOnClickListener(view -> {
            bundle.putInt("pos",2);
        });
        viewDataBinding.countYwc.setOnClickListener(view -> {
            bundle.putInt("pos",3);
        });
        viewDataBinding.countDpj.setOnClickListener(view -> {
            bundle.putInt("pos",4);
        });


    }

//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        unbinder.unbind();
//    }


    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onResume() {
        super.onResume();
//        if (Hawk.get("head") != null) {
//            myHeadImage.setImageURI(Hawk.get("head") + "");
//        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (HawkUtil.getUserId() != null) {
                viewDataBinding.userNmae.setText(Hawk.get("alias"));
                viewDataBinding.dataText.setText("编辑个人资料");
                initData(par);
            }
        }
    }
//
//    /**
//     * 客服中心
//     */
//    public void toCustomerService() {
//        MyDialog dialog = new MyDialog(getContext());
//        dialog.setTitle("温馨提示");
//        dialog.setContent("确定拨打客服电话：023-68161467？");
//        dialog.setDialogSure(dialog1 -> {
//            dialog.dismiss();
//            MyUtils.callPhone(getActivity(), "023-68161467");
//        });
//        dialog.show();
//    }
//
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        ScanResultUtils.getInstance().resultForScan(requestCode, data, getContext());
//    }
//
}
